from django.db import models

# Create your models here.
class BaseModel(models.Model):
    is_delete=models.BooleanField(default=False,verbose_name="是否删除")
    updated_time=models.DateField(auto_now_add=True,verbose_name="更新时间")
    created_time=models.DateField(auto_now=True,verbose_name="创建时间")
    class Meta():
        abstract=True

class NavigationModel(BaseModel):
    name=models.CharField(max_length=20,verbose_name="导航名称")
    link =models.CharField(max_length=50,verbose_name="导航链接")
    orders =models.IntegerField(verbose_name="序号")
    is_show=models.BooleanField(default=False,verbose_name="是否展示")
    def __str__(self):
        return self.name
    class Meta():
        db_table="Navigation"
        verbose_name_plural="导航表"


class CategoryModel(BaseModel):
    name=models.CharField(max_length=20,verbose_name="类型名称")
    pid=models.ForeignKey("self",on_delete=models.CASCADE,blank=True,null=True,verbose_name="父id")
    recomment=models.BooleanField(default=False,verbose_name="是否推荐")
#     是否推荐
    def __str__(self):
        return self.name
    class Meta():
        db_table="category"
        verbose_name_plural="分类表"


Level_CHOICE={
    (1,"初级"),
    (2,"中级"),
    (3,"高级")
}

class TeacherModel(BaseModel):
    name = models.CharField(max_length=100)
    headimage = models.CharField(max_length=200)
    info = models.TextField()
    totalcourse = models.IntegerField()
    def __str__(self):
        return self.name
    class Meta:
        db_table = 'teacher'
        verbose_name = '教师表'

class CourseModel(BaseModel):
    name=models.CharField(max_length=20,verbose_name="课程名称")
    pid=models.ForeignKey(to=CategoryModel,on_delete=models.SET_NULL,null=True)
    topid=models.IntegerField(verbose_name="一级分类id")
    level=models.IntegerField(choices=Level_CHOICE,default=1)
    src=models.CharField(max_length=200,verbose_name="图片路径")
    price=models.DecimalField(max_digits=6,decimal_places=1,verbose_name="价格")
    num=models.IntegerField(verbose_name="人数")
    is_recommend=models.BooleanField(default=False)
    is_down=models.BooleanField(default=False)

    totalJie = models.IntegerField(default=0)
    totalTime = models.IntegerField(default=0)
    info = models.TextField(null=True)
    question = models.TextField(null=True)
    teacher = models.ForeignKey(TeacherModel,on_delete=models.CASCADE,null=True,related_name="teacher")

    video=models.CharField(max_length=200,verbose_name="录像",null=True)



    def __str__(self):
        return self.name
    class Meta():
        db_table="course"
        verbose_name_plural="课程表"


# 频道表
class ChannelModel(BaseModel):
    name = models.CharField(max_length=255, unique=True)  # 假设分类名最大长度为255个字符
    picurl = models.CharField(max_length=100)
    sort = models.IntegerField()

    class Meta:
        verbose_name = '频道表'
        verbose_name_plural = '频道表'
        db_table = "channel"

    def __str__(self):
        return self.name
    # 频道分类表id  name  显示顺序  频道id   类别     分类id


class ChannelCatesModel(BaseModel):
    name = models.CharField(max_length=255, unique=True)  # 假设分类名最大长度为255个字符
    sort = models.IntegerField()
    channel = models.ForeignKey(ChannelModel, on_delete=models.CASCADE, related_name='cates')  # 自关联字段，表示父分类
    types = models.IntegerField()
    cateid = models.IntegerField()

    class Meta:
        verbose_name = '频道分类表'
        verbose_name_plural = '频道分类表'
        db_table = "channelcates"

    def __str__(self):
        return self.name
    # 频道分类课程表 id  name  图标   价格  难度  购买人数     频道分类id


class ChannelGoodsMdoel(BaseModel):
    name = models.CharField(max_length=255, unique=True)  # 假设分类名最大长度为255个字符
    picurl = models.CharField(max_length=100)
    price = models.FloatField()
    sales = models.IntegerField(default=0)
    level = models.IntegerField()
    ccates = models.ForeignKey(ChannelCatesModel, on_delete=models.CASCADE, related_name='courses')  # 自关联字段，表示父分类

    class Meta:
        verbose_name = '频道分类课程表'
        verbose_name_plural = '频道分类课程表'
        db_table = "channelcourses"

    def __str__(self):
        return self.name



class ChapterModel(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255, verbose_name='章节名称')
    course = models.ForeignKey(CourseModel, on_delete=models.CASCADE, verbose_name='课程ID',related_name="course")
    total_lessons = models.IntegerField(verbose_name='总节数')
    display_time = models.CharField(max_length=255, verbose_name='时间（页面展示）')
    total_duration_seconds = models.IntegerField(verbose_name='总时长(秒)')
    display_order = models.IntegerField(verbose_name='显示顺序')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '章表'
        verbose_name_plural = '章表'
        ordering = ['display_order']  # 根据显示顺序进行排序



class LessonModel(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255, verbose_name='节名称')
    course = models.ForeignKey(CourseModel, on_delete=models.CASCADE, verbose_name='课程ID')
    chapter = models.ForeignKey(ChapterModel, on_delete=models.CASCADE, verbose_name='章ID',related_name="chapter")
    video_id = models.CharField(max_length=255, verbose_name='视频ID')
    time = models.TimeField(verbose_name='时间')  # 假设时间是课程节的开始时间
    duration_seconds = models.IntegerField(verbose_name='时长（秒）')
    display_order = models.IntegerField(verbose_name='显示顺序')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '节表'
        verbose_name_plural = '节表'
        ordering = ['display_order']  # 根据显示顺序进行排序


class CusersModel(BaseModel):
    name = models.CharField(max_length=100,null=True)
    phone = models.CharField(max_length=100,null=True)
    pad = models.CharField(max_length=100,null=True)
    headimage = models.CharField(max_length=100,null=True)
    signature = models.CharField(max_length=100,null=True)
    score=models.IntegerField(default=0)
    def __str__(self):
        return self.name
    class Meta:
        db_table = 'cusers'
        verbose_name = '用户表'



class SFModel(BaseModel):
    users = models.ForeignKey(to=CusersModel,on_delete=models.CASCADE,related_name="users")
    types = models.CharField(max_length=225,verbose_name="三方类型")
    uid= models.CharField(max_length=225,verbose_name="用户id")
    token = models.CharField(max_length=225,verbose_name="第三方登录平台的token")
    retoken = models.CharField(max_length=225,verbose_name="第三方登录平台的token",null=True)
    def __str__(self):
        return self.uid
    class Meta:
        db_table = 'sanfang'
        verbose_name = '三方登陆表'


class CommentModel(BaseModel):
    userid = models.ForeignKey(CusersModel,on_delete=models.CASCADE,null=True,related_name="userid")
    courseid = models.ForeignKey(CourseModel,on_delete=models.CASCADE,null=True,related_name="courseid")
    message = models.TextField()
    score = models.IntegerField(default=0)
    def __str__(self):
        return self.message
    class Meta():
        db_table = "comment"
        verbose_name = '评价表'

class ReplyModel(BaseModel):
    replyid = models.IntegerField()
    commentid = models.ForeignKey(CommentModel,on_delete=models.CASCADE,null=True,related_name="commentid")
    message = models.TextField()
    def __str__(self):
        return self.message
    class Meta():
        db_table = "reply"
        verbose_name = '回复表'

class ShopCarModel(BaseModel):
    useridd=models.ForeignKey(to=CusersModel,related_name="useridd",on_delete=models.CASCADE,verbose_name="用户id")
    courseidd=models.ForeignKey(to=CourseModel,related_name="courseidd",on_delete=models.CASCADE,verbose_name="课程id")
    is_select=models.BooleanField(default=True,verbose_name="是否选择")
    # def __str__(self):
    #     return self.is_select
    class Meta():
        db_table = "shopCar"
        verbose_name = '商品购物车'


class OrdersModel(BaseModel):
    orderno = models.CharField(max_length=255, primary_key=True)
    userid = models.ForeignKey(CusersModel, on_delete=models.CASCADE, verbose_name='用户id')  # 注意：这里通常使用外键关联User模型
    status = models.IntegerField(choices=((1, '生成'), (2, '已支付'), (3, '支付失败')), verbose_name='支付状态')
    orders_status = models.IntegerField(
        choices=((1, '生成'), (2, '已支付'), (3, '支付失败'), (4, '退款'), (5, '评价'), (6, '已经完成')),
        verbose_name='订单状态')
    transaction = models.CharField(max_length=50, verbose_name='流水号')
    pay_type = models.IntegerField(choices=((1, '支付宝'), (2, '微信'), (3, '网银')), verbose_name='支付类型')
    score = models.IntegerField(default=0)
    couponid = models.IntegerField(default=0)
    couponmoney = models.IntegerField(default=0)
    tmoney = models.IntegerField(default=0)
    pay_money = models.IntegerField(default=0)

    class Meta:
        verbose_name = '订单'
        verbose_name_plural = '订单'
        db_table = "order"

    def __str__(self):
        return f'订单ID: {self.orderno}'


class OrdersDetailModel(BaseModel):
    user = models.ForeignKey(CusersModel, on_delete=models.CASCADE,related_name="userorder")
    orders = models.ForeignKey(OrdersModel, to_field='orderno', on_delete=models.CASCADE,related_name="detailorder")
    course = models.ForeignKey(CourseModel, on_delete=models.CASCADE,related_name="ordercourse")
    name = models.CharField(max_length=255)  # 假设分类名最大长度为255个字符
    picurl = models.CharField(max_length=200, default='')
    price = models.FloatField()
    comment_status = models.IntegerField(default=0)

    def __str__(self):
        return self.name
    class Meta:
        verbose_name = '订单详情表'
        verbose_name_plural = '订单详情表'
        db_table = "order_detail"


class CouponModel(BaseModel):
    name=models.CharField(max_length=30,verbose_name="优惠券名称")
    max=models.IntegerField(default=0,verbose_name="满")
    out=models.IntegerField(default=0,verbose_name="减")
    startTime=models.DateField(verbose_name="优惠开始时间")
    endTime=models.DateField(verbose_name="优惠结束时间")
    totalCount=models.IntegerField(default=0,verbose_name="总数")
    userdCount=models.IntegerField(default=0,verbose_name="已经使用的")

    class Meta():
        db_table="coupon"
        verbose_name="优惠券表"
        verbose_name_plural="优惠券表"

class UserCouponModel(BaseModel):
    user=models.ForeignKey(to=CusersModel,on_delete=models.SET_NULL,null=True,verbose_name="用户")
    #已使用，已过期，可使用
    userStatus=models.IntegerField(choices=((1, '可使用'), (2, '已使用'), (3, '已过期')),default=1)
    coupon=models.ForeignKey(to=CouponModel,on_delete=models.CASCADE,verbose_name="优惠券",related_name="coupon")
    enable_course=models.CharField(max_length=200,verbose_name="是否选择全部",default="__all__")
    class Meta():
        db_table="usercoupon"
        verbose_name="用户优惠券表"
        verbose_name_plural="用户优惠券表"


class UserCourserCannal(BaseModel):
    user = models.ForeignKey(to=CusersModel, on_delete=models.SET_NULL, null=True, verbose_name="用户")
    course = models.ForeignKey(CourseModel, on_delete=models.CASCADE)
    class Meta():
        db_table="usercoursecannal"
        verbose_name="用户课程取消表"
        verbose_name_plural="用户课程取消表"


class UserCourseModel(models.Model):
    userid = models.ForeignKey(CusersModel,on_delete=models.CASCADE,default=None,null=True,related_name='usercourse')
    courseid = models.ForeignKey(CourseModel,on_delete=models.CASCADE,default=None,null=True,related_name='coursecourse')
    progress = models.DecimalField(max_digits=4, decimal_places=2, default=0)
    def __str__(self):
        return self.id
    class Meta:
        db_table = 'usercourse'
        verbose_name = '用户课程表'


class UserChapLessonModel(models.Model):
    name = models.CharField(max_length=100,default=None,null=True)
    userid = models.ForeignKey(CusersModel,on_delete=models.CASCADE,default=None,null=True,related_name='userchap')
    courseid = models.ForeignKey(CourseModel,on_delete=models.CASCADE,default=None,null=True,related_name='coursechap')
    progress = models.DecimalField(max_digits=4, decimal_places=2, default=0)
    url = models.CharField(max_length=200,default=None,null=True)
    status = models.IntegerField(choices=((1, '未学'), (2, '学习中'), (3, '已学完')), verbose_name='学习状态')
    def __str__(self):
        return self.id
    class Meta:
        db_table = 'userchaplesson'
        verbose_name = '用户课程表'

class TestModel(BaseModel):
    testno = models.CharField(max_length=255, primary_key=True)
    inputcontent=models.CharField(max_length=255)
    inputmethod=models.CharField(max_length=255,null=True)
    inputurl=models.CharField(max_length=255,null=True)
    result_yuce=models.CharField(max_length=255)
    result_end=models.CharField(max_length=255)
    level=models.IntegerField(default=0)
    success=models.BooleanField(default=False)
    def __str__(self):
        return self.testno
    class Meta:
        db_table = 'testPhone'
        verbose_name = '测试手机号表'



